from PyQt6.QtCore import QSettings
from PyQt6.QtGui import QPalette, QColor
from PyQt6.QtWidgets import QApplication

class StyleManager:
    """样式管理器类，用于管理应用程序的主题和样式"""
    
    LIGHT_THEME = {
        'bg_primary': '#ffffff',
        'bg_secondary': '#f8f9fa',
        'border': '#e9ecef',
        'text_primary': '#212529',
        'text_secondary': '#495057',
        'accent': '#4dabf7',
        'accent_hover': '#339af0',
        'accent_pressed': '#228be6',
        'danger': '#fa5252',
        'warning': '#ffd43b',
        'success': '#51cf66',
        'table_header': '#f1f3f5',
        'table_row_alt': '#f8f9fa',
        'table_row_hover': '#e9ecef',
        'input_bg': '#ffffff',
        'input_border': '#ced4da',
        'input_border_focus': '#4dabf7',
        'button_secondary_bg': '#f8f9fa',
        'button_secondary_border': '#ced4da',
        'button_secondary_text': '#495057',
    }
    
    DARK_THEME = {
        'bg_primary': '#212529',
        'bg_secondary': '#343a40',
        'border': '#495057',
        'text_primary': '#f8f9fa',
        'text_secondary': '#ced4da',
        'accent': '#339af0',
        'accent_hover': '#228be6',
        'accent_pressed': '#1c7ed6',
        'danger': '#ff6b6b',
        'warning': '#ffd43b',
        'success': '#69db7c',
        'table_header': '#343a40',
        'table_row_alt': '#2b3035',
        'table_row_hover': '#495057',
        'input_bg': '#343a40',
        'input_border': '#495057',
        'input_border_focus': '#339af0',
        'button_secondary_bg': '#343a40',
        'button_secondary_border': '#495057',
        'button_secondary_text': '#ced4da',
    }
    
    @staticmethod
    def get_theme_colors(theme_name='浅色'):
        """获取主题颜色"""
        if theme_name == '深色':
            return StyleManager.DARK_THEME
        return StyleManager.LIGHT_THEME
    
    @staticmethod
    def apply_theme(app, theme_name='浅色'):
        """应用主题到应用程序"""
        colors = StyleManager.get_theme_colors(theme_name)
        
        # 设置应用程序样式表
        app.setStyleSheet(f"""
            /* 全局样式 */
            QWidget {{
                background-color: {colors['bg_primary']};
                color: {colors['text_primary']};
            }}
            
            /* 主窗口样式 */
            QMainWindow {{
                background-color: {colors['bg_secondary']};
            }}
            
            /* 菜单栏样式 */
            QMenuBar {{
                background-color: {colors['bg_primary']};
                border-bottom: 1px solid {colors['border']};
            }}
            
            QMenuBar::item {{
                padding: 8px 12px;
                background-color: transparent;
            }}
            
            QMenuBar::item:selected {{
                background-color: {colors['accent']};
                color: white;
            }}
            
            /* 菜单样式 */
            QMenu {{
                background-color: {colors['bg_primary']};
                border: 1px solid {colors['border']};
                padding: 5px;
            }}
            
            QMenu::item {{
                padding: 8px 25px 8px 20px;
                border-radius: 4px;
                margin: 2px;
            }}
            
            QMenu::item:selected {{
                background-color: {colors['accent']};
                color: white;
            }}
            
            /* 工具栏样式 */
            QToolBar {{
                background-color: {colors['bg_primary']};
                border-bottom: 1px solid {colors['border']};
                padding: 5px;
            }}
            
            QToolButton {{
                border: none;
                border-radius: 4px;
                padding: 5px;
            }}
            
            QToolButton:hover {{
                background-color: {colors['button_secondary_bg']};
            }}
            
            QToolButton:pressed {{
                background-color: {colors['accent']};
            }}
            
            /* 状态栏样式 */
            QStatusBar {{
                background-color: {colors['bg_primary']};
                border-top: 1px solid {colors['border']};
                color: {colors['text_secondary']};
            }}
            
            /* 表格样式 */
            QTableWidget {{
                background-color: {colors['bg_primary']};
                border: 1px solid {colors['border']};
                gridline-color: {colors['border']};
            }}
            
            QTableWidget::item {{
                padding: 8px;
                border: none;
            }}
            
            QTableWidget::item:selected {{
                background-color: {colors['accent']};
                color: white;
            }}
            
            QHeaderView::section {{
                background-color: {colors['table_header']};
                color: {colors['text_primary']};
                padding: 8px;
                border: none;
                border-right: 1px solid {colors['border']};
                border-bottom: 1px solid {colors['border']};
            }}
            
            /* 输入框样式 */
            QLineEdit, QTextEdit, QSpinBox, QComboBox {{
                background-color: {colors['input_bg']};
                border: 1px solid {colors['input_border']};
                border-radius: 6px;
                padding: 8px;
                color: {colors['text_primary']};
            }}
            
            QLineEdit:focus, QTextEdit:focus, QSpinBox:focus, QComboBox:focus {{
                border-color: {colors['input_border_focus']};
            }}
            
            /* 按钮样式 */
            QPushButton {{
                border-radius: 6px;
                padding: 8px 16px;
                background-color: {colors['button_secondary_bg']};
                border: 1px solid {colors['button_secondary_border']};
                color: {colors['button_secondary_text']};
            }}
            
            QPushButton:hover {{
                background-color: {colors['accent']};
                border-color: {colors['accent']};
                color: white;
            }}
            
            QPushButton:pressed {{
                background-color: {colors['accent_pressed']};
                border-color: {colors['accent_pressed']};
            }}
            
            QPushButton#primaryButton {{
                background-color: {colors['accent']};
                border: none;
                color: white;
                font-weight: bold;
            }}
            
            QPushButton#primaryButton:hover {{
                background-color: {colors['accent_hover']};
            }}
            
            QPushButton#primaryButton:pressed {{
                background-color: {colors['accent_pressed']};
            }}
            
            /* 复选框样式 */
            QCheckBox {{
                spacing: 8px;
            }}
            
            QCheckBox::indicator {{
                width: 18px;
                height: 18px;
                border-radius: 4px;
                border: 1px solid {colors['input_border']};
                background-color: {colors['input_bg']};
            }}
            
            QCheckBox::indicator:checked {{
                background-color: {colors['accent']};
                border-color: {colors['accent']};
            }}
            
            QCheckBox::indicator:hover {{
                border-color: {colors['accent']};
            }}
            
            /* 标签页样式 */
            QTabWidget::pane {{
                border: 1px solid {colors['border']};
                border-radius: 8px;
                background-color: {colors['bg_primary']};
            }}
            
            QTabBar::tab {{
                background-color: {colors['bg_secondary']};
                border: 1px solid {colors['border']};
                border-bottom: none;
                border-top-left-radius: 8px;
                border-top-right-radius: 8px;
                padding: 8px 16px;
                margin-right: 2px;
                color: {colors['text_secondary']};
            }}
            
            QTabBar::tab:selected {{
                background-color: {colors['bg_primary']};
                border-bottom: 2px solid {colors['accent']};
                color: {colors['text_primary']};
                font-weight: bold;
            }}
            
            QTabBar::tab:hover:!selected {{
                background-color: {colors['button_secondary_bg']};
            }}
            
            /* 分组框样式 */
            QGroupBox {{
                font-weight: bold;
                border: 1px solid {colors['border']};
                border-radius: 8px;
                margin-top: 12px;
                padding-top: 24px;
            }}
            
            QGroupBox::title {{
                subcontrol-origin: margin;
                subcontrol-position: top left;
                left: 12px;
                padding: 0 5px;
                color: {colors['text_primary']};
                background-color: {colors['bg_primary']};
            }}
            
            /* 进度条样式 */
            QProgressBar {{
                border: 1px solid {colors['border']};
                border-radius: 8px;
                text-align: center;
                background-color: {colors['bg_secondary']};
            }}
            
            QProgressBar::chunk {{
                border-radius: 7px;
            }}
            
            /* 滚动条样式 */
            QScrollBar:vertical {{
                border: none;
                background-color: {colors['bg_secondary']};
                width: 12px;
                margin: 0;
            }}
            
            QScrollBar::handle:vertical {{
                background-color: {colors['button_secondary_border']};
                border-radius: 6px;
                min-height: 30px;
                margin: 2px;
            }}
            
            QScrollBar::handle:vertical:hover {{
                background-color: {colors['accent']};
            }}
            
            QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{
                height: 0;
            }}
            
            QScrollBar:horizontal {{
                border: none;
                background-color: {colors['bg_secondary']};
                height: 12px;
                margin: 0;
            }}
            
            QScrollBar::handle:horizontal {{
                background-color: {colors['button_secondary_border']};
                border-radius: 6px;
                min-width: 30px;
                margin: 2px;
            }}
            
            QScrollBar::handle:horizontal:hover {{
                background-color: {colors['accent']};
            }}
            
            QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal {{
                width: 0;
            }}
        """)
        
        # 设置调色板
        palette = QPalette()
        palette.setColor(QPalette.ColorRole.Window, QColor(colors['bg_primary']))
        palette.setColor(QPalette.ColorRole.WindowText, QColor(colors['text_primary']))
        palette.setColor(QPalette.ColorRole.Base, QColor(colors['bg_primary']))
        palette.setColor(QPalette.ColorRole.AlternateBase, QColor(colors['bg_secondary']))
        palette.setColor(QPalette.ColorRole.Text, QColor(colors['text_primary']))
        palette.setColor(QPalette.ColorRole.Button, QColor(colors['button_secondary_bg']))
        palette.setColor(QPalette.ColorRole.ButtonText, QColor(colors['button_secondary_text']))
        palette.setColor(QPalette.ColorRole.Highlight, QColor(colors['accent']))
        palette.setColor(QPalette.ColorRole.HighlightedText, QColor('white'))
        
        app.setPalette(palette)